//
//  111
//
//  Created by apple on 2021/10/26. 优化痛风分型bug 3.4.4
//  Copyright © 2021 zglee. All rights reserved.
//mj血糖记录（知痛风）

#import "MJBloodSugarRecordVC.h"
#import "MJBloodSugarDetectionResultVC.h"
#import "MJBloodSugarDetectionResultVC.h"
#import "GoutRecordListCell.h"
#import "GoutRecordListHeaderCell.h"

@interface MJBloodSugarRecordVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MJUITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) NSString *dateTime;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, assign) NSInteger total;
@end

@implementation MJBloodSugarRecordVC

- (void)viewDidLoad{
    self.backgroundColorType = BackgroundColorTypeGray;
    [super viewDidLoad];
    
    MJWeakSelf
    if(self.isFromMember){
        MJUIAfter_GCD(MJUIRecordUpdateTime, ^{
            [weakSelf processData:[WCBDHelper.shared mj_getBloodSugarArrayWithParm:[weakSelf getParmModel]]];
        });
    }else{
        [[RACObserve(HealthHelper.shared, parmModel) delay:MJUIRecordUpdateTime] subscribeNext:^(MJCommonModel *parmModel) {
            weakSelf.patientId = parmModel.patientId;
            [weakSelf processData:[WCBDHelper.shared mj_getBloodSugarArrayWithParm:parmModel]];
        }];
    }
    
    if(WCBDHelper.shared.isBloodSugarUpdated){
        [WCBDHelper.shared requestBloodSugarListDataWithLoading:true dataArrayBlock:^(NSArray *array) {
            [weakSelf processData:array];
        }];
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)mj_setUI{
    [super mj_setUI];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-[MJHelper safeAreaBottom]);
     }];
}

#pragma mark - 方法
- (void)requestData{
    MJWeakSelf
    [WCBDHelper.shared requestBloodSugarListDataWithLoading:false dataArrayBlock:^(NSArray *array) {
        [weakSelf processData:[WCBDHelper.shared mj_getBloodSugarArrayWithParm:[weakSelf getParmModel]]];
    }];
}

- (void)processData:(NSArray *)modelArray{
    
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_footer resetNoMoreData];
    [self.dataSourceArray removeAllObjects];
    [self.dataSourceArray addObjectsFromArray:modelArray];
    if(self.dataSourceArray.count == self.total){
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    self.pageNumber++;
    NSMutableArray *array = [HealthHelper itemMonthGroupArray:self.dataSourceArray aClass:MJGoutDetectionModel.class];
    self.dataArray = array;
    [self.tableView reloadData];
    [self showEmptyView];
}

- (void)showEmptyView{
    if(self.dataArray.count>0){
        [self hideEmptyView:self.tableView];
        self.tableView.mj_footer.hidden = false;
    }else{
        self.tableView.mj_footer.hidden = true;
        [self showEmptyView:self.tableView emptyStyle:EmptyViewRecord];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *array = self.dataArray[section][@"array"];
    return array.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GoutRecordListCell *cell = [GoutRecordListCell reuseForTableView:tableView forIndexPath:indexPath];
    cell.detectionItemType = DetectionItemTypeGlu;
    NSMutableArray *array = self.dataArray[indexPath.section][@"array"];
    MJBloodSugarModel *model = array[indexPath.row];
    cell.bloodSugarModel = model;
    cell.isCornerRadiusBottom = (indexPath.row == array.count - 1) ? true:false;
    cell.itemMemberView.hidden = self.isFromMember;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *array = self.dataArray[indexPath.section][@"array"];
    MJBloodSugarModel *model = array[indexPath.row];
    MJBloodSugarDetectionResultVC *vc = [[MJBloodSugarDetectionResultVC alloc]initWithModel:model];
    [self.navigationController pushViewController:vc animated:true];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35+18;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]init];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    GoutRecordListHeaderCell *sectionHeaderView = [GoutRecordListHeaderCell reuseForTableView:tableView];
    NSString *month = self.dataArray[section][@"month"];
    sectionHeaderView.titleLabel.text = month;
    sectionHeaderView.detectionItemType = DetectionItemTypeGlu;
    sectionHeaderView.itemMemberView.hidden = self.isFromMember;
    return sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

#pragma mark - JXCategoryListContentViewDelegate 代理
- (UIView *)listView{
    return self.view;
}

#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)dataSourceArray {
    if (_dataSourceArray == nil) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (MJUITableView *)tableView{
    if (!_tableView){
        MJWeakSelf
        MJUITableView *tableView = [[MJUITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 160;
        tableView.contentInset= UIEdgeInsetsMake(0,0, 10, 0);
        [GoutRecordListCell registerNibWithTableView:tableView];
        [GoutRecordListHeaderCell registerNibWithTableView:tableView];
        tableView.mj_header = [MJUIRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf requestData];
        }];
        _tableView = tableView;
    }
    return _tableView;
}

@end
